package com.cy.personer.web;

import com.alibaba.fastjson.JSONObject;
import com.cy.personer.bean.Advert;
import com.cy.personer.bean.ReportItem;
import com.cy.personer.bean.Suggest;
import com.cy.personer.biz.AdvertBiz;
import com.cy.personer.biz.SuggestBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

/**
 * 广告
 */
@Controller
@RequestMapping("/advert")
public class AdvertHandler {

    @Resource
    AdvertBiz advertBizImpl;

    @Autowired
    Advert advert;

    // 修改广告
    @RequestMapping(value = "/updateAdvert.action", method= RequestMethod.POST, produces="text/html;charset=utf-8")
    public @ResponseBody
    String updateAdvert(Advert advert){
        return ""+advertBizImpl.updateAdvert(advert);
    }

    //新增广告
    @RequestMapping(value = "/addAdvert.action", method= RequestMethod.POST, produces="text/html;charset=utf-8")
    public @ResponseBody
    String addAdvert(Advert advert){
        return ""+advertBizImpl.addAdvert(advert);
    }

    //查找广告列表
    @RequestMapping(value = "/getAdverts.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody
    PageInfo<Advert> getAdverts(Advert advert,int pageNum){
        return advertBizImpl.getAdverts(advert,pageNum);
    }

    //查找广告
    @RequestMapping(value = "/getAdvert.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody
    Advert getAdvert(Advert advert){
        return advertBizImpl.getAdvert(advert);
    }

    /**
     * 删除广告
     * @author 林少艺
     */
    @RequestMapping(value = "/delAdvert.action", method= RequestMethod.POST, produces="text/html;charset=utf-8")
    public @ResponseBody
    String delAdvert(Advert advert){
        return ""+ advertBizImpl.delAdvert(advert);
    }

    //上传图片
    @RequestMapping("/upload.action")
    public @ResponseBody Advert upload(HttpSession session, MultipartFile add_file) throws Exception {
        //设置文件保存的路径
        String path = session.getServletContext().getRealPath("/adImg");
        //获取文件名
        String fileName = add_file.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
        //得到时间戳
        String str = "" + System.currentTimeMillis();
        //组装文件名（带后缀）
        fileName = str + "."+prefix;
        //如果目录不存在就新建目录
        File folder = new File(path);
        if  (!folder.exists()  && !folder.isDirectory()) {
            folder .mkdir();
        }
        //path指定到文件夹，prefix（文件名.后缀）
        File file1 = new File(path,fileName);
        //保存文件
        add_file.transferTo(file1);
        advert.setUrl("adImg/"+fileName);
        return advert;
    }
   @RequestMapping("/quer.action")
    public String quer(HttpServletRequest request){
       List<Advert>list;
       list=advertBizImpl.quers();
       if (list!=null){
           request.setAttribute("list",list);
           return "user/userIndex";
       }else
           return "";
    }

}
